package com.example.controller;


import com.example.model.auto.Course;
import com.example.model.auto.Dynamic;
import com.example.service.ICourseService;
import com.example.service.IDynamicService;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 达瓦里氏
 * @since 2021-12-07
 */
@RestController
@RequestMapping("/dynamic")
@CrossOrigin
public class DynamicController {

    @Resource
    private IDynamicService iDynamicService;

    /*查询课程*/
    @GetMapping("/getDynamic")
    public List<Dynamic> getDynamic(){
        return iDynamicService.list();
    }

    /*通过id查询课程*/
    @GetMapping("/getDynamicById/{id}")
    public Dynamic getDynamicById(@PathVariable("id") int id){
        return iDynamicService.getById(id);
    }

    /*修改课程*/
    @PostMapping("/updateDynamic")
    public void updateDynamic(@RequestBody Dynamic dynamic){
        iDynamicService.updateById(dynamic);
    }

    /*添加课程*/
    @PostMapping("/setDynamic")
    public void setDynamic(@RequestBody Dynamic dynamic){ iDynamicService.save(dynamic); }

    /*删除课程*/
    @PostMapping("/deletedById/{id}")
    public boolean deleted(@PathVariable("id") int id){ return iDynamicService.removeById(id); }


    /*图片上传*/
    @PostMapping("/elupload")
    public String elUpload(MultipartFile file){
        String fileName = file.getOriginalFilename();
        File dest = new File("D:\\project\\study\\vue\\static\\Dynamic_img\\" + fileName);
        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败！";
    }

}
